package com.archstoday.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.archstoday.BaseApp;
import com.archstoday.R;
import com.archstoday.utils.BitmapUtil;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService ;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 宣传启动页
 */
public class StartUpActivity extends AsBaseActivity {

    private ViewPager mPager;
    private TextView mToActivityBtn;

//    private Bitmap[] bitmaps;
    private List<Bitmap> bitmapList;

    private ScheduledExecutorService singleThreadPool;
    private int time = 3;

    private MyTimeTask mTimeTask;
    private Timer timer;
    private int recLen;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_start_up);
        mPager = (ViewPager) findViewById(R.id.view_pager);
        mToActivityBtn = (TextView) findViewById(R.id.to_activity_btn);
    }

    @Override
    protected void initData(Bundle bundle) {
//        bitmaps = BitmapUtil.getInstance().getBitmaps();
        bitmapList = BitmapUtil.getInstance().getBitmapList();
        mPager.setAdapter(adapter);

//        singleThreadPool = Executors.newSingleThreadScheduledExecutor();
//        singleThreadPool.scheduleAtFixedRate(new Runnable() {
//            @Override
//            public void run() {
//                if (time > 0) {
//                    mToActivityBtn.setText(time + "s");
//                    time--;
//                } else {
//                    singleThreadPool.shutdownNow();
//                    toActivity();
//                }
//            }
//        }, 1, 1, TimeUnit.SECONDS);
        if (mTimeTask != null) {
            mTimeTask.cancel();
        }
        timer = new Timer();
        mTimeTask = new MyTimeTask();
        recLen = 3;
        timer.schedule(mTimeTask, 1000, 1000);
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.to_activity_btn);

        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
//                if (position == (adapter.getCount() - 1)) {
                    mToActivityBtn.setVisibility(View.VISIBLE);
//                } else {
//                    mToActivityBtn.setVisibility(View.GONE);
//                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void treatClickEvent(int id) {
        switch (id) {
            case R.id.to_activity_btn:
                getEvent(BaseApp.E_START_UP, BaseApp.PAGE_TYPE_START_UP);
//                singleThreadPool.shutdownNow();
                if (mTimeTask != null) {
                    mTimeTask.cancel();
                }
                toActivity();
                break;
        }
    }

    private void toActivity() {
        Intent intent = new Intent(mContext, MainActivity.class);
        startActivity(intent);
        appFinish();
    }

    PagerAdapter adapter=new PagerAdapter(){

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return bitmapList.size();
//            return bitmaps.length;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0==arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object o){
            //container.removeViewAt(position);
        }

        //设置ViewPager指定位置要显示的view
        @Override
        public Object instantiateItem(ViewGroup container, int position){
            ImageView im = new ImageView(mContext);
            im.setScaleType(ImageView.ScaleType.CENTER_CROP);
//            ImageUtil.setCropBitmap(mContext, im,
//                    mEntity.getData().get(position).getLabel(), R.mipmap.default_icon);
            im.setImageBitmap(bitmapList.get(position));//bitmaps[position]);
            container.addView(im);
            return im;
        }

    };

    class MyTimeTask extends TimerTask {
        @Override
        public void run() {
            Message message = new Message();
            handler.sendMessage(message);
        }
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            if(recLen == 0){
                if (mTimeTask != null) {
                    mTimeTask.cancel();
                }
                timer.cancel();
                toActivity();
            }
            mToActivityBtn.setText(recLen + "s");
            recLen--;
        }
    };
}